
/*!
 * \file     scoremanager.cpp
 * \brief    ScoreManager implementation.
 * \author   Duriez Franck
 * \author   Carton Paul-Henri
 * \version  0.1
 */

#include "assert_macros.h"
#include "constant.h"
#include "scoremanager.h"
#include <sstream>

static float f(int lines); ///< \brief Point per line

ScoreManager::ScoreManager(QSFMLWidget *displayer, float *dt) : /*{{{*/
		Drawable(displayer, displayer, dt),
		m_score_displayer("0",
				SCORE_DISPLAYER_FONT,
				SCORE_DISPLAYER_FONT_SIZE),
		m_background(sf::Shape::Rectangle(0, 0,
					SCORE_DISPLAYER_SIZE_X,
					SCORE_DISPLAYER_SIZE_Y,
					SCORE_BACKGROUND_COLOR,
					OUTLINE,
					OUTLINE_COLOR)),
		m_score_efficient(0.f),
		m_score(0.f),
		m_level(1),
		raiseScoreAction(&ScoreManager::raiseScoreStartingProcess,
				&ScoreManager::raiseScoreProcess,
				&ScoreManager::raiseScoreStoppingProcess,
				this)
{
	m_score_displayer.SetPosition(
			SCORE_POS_X,
			SCORE_POS_Y);
	m_background.SetPosition(
			SCORE_BACKGROUND_POS_X,
			SCORE_BACKGROUND_POS_Y);
} /*}}}*/

int ScoreManager::getLevel() const /*{{{*/
{
	return m_level;
} /*}}}*/

void ScoreManager::raiseScore(int linesdestroyed) /*{{{*/
{
	// If no line destroyed => exit
	if (linesdestroyed == 0) return;

	// score raising
	m_score_efficient += f(linesdestroyed) * (m_level);
	raiseScoreAction.restart(false);

	// Checking level raising
	if (m_score > m_level * LEVEL_UP) /*{{{*/
	{
		++m_level;
		std::cout << "levelup : " << m_level << std::endl;
		emit levelUp();
	} /*}}}*/
} /*}}}*/

static float f(int lines) /*{{{*/
{
	float res = 0.f;

	switch (lines) /*{{{*/
	{
		case 0:
			break;
		case 1:
			res = 40.f;
			break;
		case 2:
			res = 100.f;
			break;
		case 3:
			res = 300.f;
			break;
		default: // 4
			res = 1200.f * (lines - 3);
			break;
	} /*}}}*/

	return res;
} /*}}}*/

/************
 * DRAWABLE *
 ************/
/*{{{*/
void ScoreManager::draw() const /*{{{*/
{
	displayer->Draw(m_background);
	displayer->Draw(m_score_displayer);
} /*}}}*/

void ScoreManager::update() /*{{{*/
{
	raiseScoreAction.update();
	std::ostringstream score;
	score << (int) m_score;
	m_score_displayer.SetText(score.str());
} /*}}}*/

void ScoreManager::setCenter(Vector2f) /*{{{*/
{
} /*}}}*/

void ScoreManager::setPosition(Vector2f) /*{{{*/
{
} /*}}}*/

void ScoreManager::setRotation(float) /*{{{*/
{
} /*}}}*/

void ScoreManager::moveCenter(Vector2f) /*{{{*/
{
} /*}}}*/

void ScoreManager::move(Vector2f) /*{{{*/
{
} /*}}}*/

void ScoreManager::rotate(float) /*{{{*/
{
} /*}}}*/

Vector2f ScoreManager::getCenter() const /*{{{*/
{
	return Vector2f(0,0);
} /*}}}*/

Vector2f ScoreManager::getPosition() const /*{{{*/
{
	return Vector2f(0,0);
} /*}}}*/

float ScoreManager::getRotation() const /*{{{*/
{
	return 0.f;
} /*}}}*/

std::string ScoreManager::toString() const /*{{{*/
{
	/// \todo Implement it.
	return std::string();
} /*}}}*/
/*}}}*/

/**********
 * ACTION *
 **********/
/*{{{*/
void ScoreManager::raiseScoreStartingProcess() /*{{{*/
{
	// nothing to do
} /*}}}*/

void ScoreManager::raiseScoreProcess() /*{{{*/
{
	if (m_score_efficient > m_score + PTS_PER_SEC * (*dt)) /*{{{*/
	{
		raiseScoreAction.maintain();
		m_score += PTS_PER_SEC*(*dt);
	} /*}}}*/
} /*}}}*/

void ScoreManager::raiseScoreStoppingProcess()
{
	m_score = m_score_efficient;
}
/*}}}*/
